package com.demo.web.common;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.demo.form.UserForm;
import com.demo.service.common.CommonService;

@Controller
public class CommonController {

	@Autowired
	CommonService  commonService;
	
	@RequestMapping(value="index.htm")
	 public ModelAndView getIndex(){
		 ModelAndView mav=new ModelAndView("index");
		 mav.addObject("userForm", new UserForm());
		 return mav;
	 }
	
	@RequestMapping(value="registerUser.htm")
	public ModelAndView submitUser(@ModelAttribute UserForm userForm,BindingResult result){
		 ModelAndView mav=new ModelAndView();
		 if(result.hasErrors()){
			 
			 mav.setViewName("index");
			 
		 }else {
			 mav.setViewName("registerSucces");
			 
			  commonService.registerUser(userForm);
			 
		 }
		 return mav;
	}
	
	
	
	@PostConstruct
	public void init(){
		System.out.println("IN init controller..");
	}
	
}
